package doublepointer;

/**
 * Created by lh on 2019/7/22.
 * 题目描述：
 * 可以删除一个字符，判断是否能构成回文字符串。
 *
 * Example:
 * Input: "abca"
 * Output: True
 * Explanation: You could delete the character 'c'.
 */
public class Palindrome {

    public static boolean validPalindrome(String str) {
        for (int i = 0, j = str.length() - 1; i < j; i++, j--) {
            if (str.charAt(i) != str.charAt(j)) {
                if (isPalindrome(str, i, j - 1)) {
                    System.out.println("should delete '" + str.charAt(j) + "'");
                    return true;
                } else if (isPalindrome(str, i + 1, j)) {
                    System.out.println("should delete '" + str.charAt(i) + "'");
                    return true;
                } else {
                    System.out.println("is not a palindrome string");
                    return false;
                }
            }
        }
        return true;
    }

    public static boolean isPalindrome(String str, int left, int right) {
        while (left < right) {
            if (str.charAt(left++) != str.charAt(right++)) {
                return false;
            }
        }
        return true;
    }
}
